(() => {
    const btn = document.createElement("button")
    document.body.appendChild(btn);
    btn.style.position = "fixed";
    btn.style.right = "200px";
    btn.style.bottom = "200px";
    const submitBtn = document.createElement("button")
    submitBtn.style.position = "fixed"
    submitBtn.style.right = "100px";
    submitBtn.style.bottom = "100px";
    submitBtn.innerText = "提交并保存"
    document.body.appendChild(submitBtn)
    let stat = false;
    let time;
    const getTable = () => {
        const body = document.getElementsByClassName("ant-table-tbody")[0];
        const array = Array.from(body.children).map((line) => {
            const [context, dom] = Array.from(line.children);
            const [title, value] = Array.from(context.children[0].children[0].children);
            const [commentId, videoId] = title.innerText.split("\t");
            return {
                commentId,
                videoId,
                value: value.innerText,
                dom,
                unionid: `${commentId}${videoId}`
            }
        })
        return array;
    }
    const queue = [];
    const setData = (unionid, value, dom) => {
        const labels = Array.from(dom.getElementsByTagName("label"));
        const index = labels.findIndex(e => e.className.includes("checked"))
        if (index == -1) return;
        fetch(`http://114.115.166.23:14520/qq/xsyj/${encodeURI(unionid)}/${encodeURI(value)}/${index}/set`);
    }
    const getData = (arr) => {
        for (const { unionid, dom } of arr) {
            fetch(`http://114.115.166.23:14520/qq/xsyj/${encodeURI(unionid)}/get`, {
                method: "GET"
            }).then(e => e.json()).then((e) => {
                const type = new Array(100).fill(0);
                for (const i of e) type[i]++;
                const labels = Array.from(dom.getElementsByTagName("label"));
                const spans = labels.map(e => e.children[1]);
                for (let i = 0; i < spans.length; i++) {
                    spans[i].innerText = [spans[i].innerText.split("-")[0], type[i]].join("-")
                }
            })
        }
    }
    const onSubmit = () => {
        if (!queue.length) return;
        const data = queue[0];
        queue.shift();
        setData(data);
    }
    const start = () => {
        btn.innerText = "暂停"
        const hash = new Set();
        time = setInterval(() => {
            const array = getTable();
            const result = array.filter((e) => {
                if (hash.has(e.unionid)) return false;
                hash.add(e.unionid);
                return true;
            })
            for (const item of result) {
                queue.push(item);
            }
            getData(result);
            clearTimeout(time);
        }, 1000);
    }
    const end = () => {
        btn.innerText = "点击开始"
        clearInterval(time);
    }
    const click = () => {
        stat = !stat;
        if (stat) start()
        else end();
    }
    btn.onclick = click;
    const onFinish = () => {
        onSubmit()
        setTimeout(() => {
            Array.from(document.getElementsByTagName("button")).find(e => e.innerText.includes("提交")).click()
        }, 500)
    }
    submitBtn.onclick = onFinish
    document.addEventListener("keydown", function (event) {
        if (event.key === " ") {
            // 执行你想要执行的函数
            onFinish()
        }
    });
    end()
})()